#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "testutils.h"

void steward_test()
{
  int i, k, j, pass, choice1 = 0, choice2 = -1, choice3 = -1, card1, card2, numPlayers, coin_bonus;
  struct gameState state;

  for(i = 0; i < MAX_ITERATIONS; i++){
    coin_bonus = 0;
    pass = 0;

    numPlayers = 1;

    init_test_data(&state, numPlayers);

    srand(time(NULL));

    for(k = 0; k < numPlayers; k++){
      // set deck
      state.deckCount[k] = 50;
      for(j = 0; j < state.deckCount[k]; j++){
        state.deck[k][j] = random_card();
      }

      // set hand
      state.handCount[k] = 5;
      for(j = 0; j < state.handCount[k]; j++){
        state.hand[k][j] = random_card();
	while(state.hand[k][j] == steward)
	  state.hand[k][j] = random_card();
      }
    }
    state.hand[0][4] = steward;

    // choose which action to take (1, 2, or 3)
    choice1 = (rand() % 3) + 1;
 
    // choose which cards to discard
    while(choice2 != -1 && choice3 != -1 && choice2 != choice3){
      choice2 = rand() % 4;
      choice3 = rand() % 4;
    }

    card1 = state.hand[0][choice2];
    card2 = state.hand[0][choice3];

    printf("ITERATION %d\n", i);

    if(cardEffect(state.hand[0][4], choice1, choice2, choice3, &state, 4, &coin_bonus) != 0)
      printf("Failure, steward effect not returned\n");
    else
      pass++;

    if(choice1 == 1){
      if(state.handCount[0] == 7)
	pass++;
      else
	printf("Failure, player did not gain 2 cards\n");
    }
    if(choice1 == 2){
      if(state.coins == 2)
	pass++;
      else
	printf("Failure, player did not gain 2 coins\n");
    }
    if(choice1 == 3){
      if(card1 == card2){
	if(card_count(state.discard[0], card1, 3) == 2)
	  pass+=2;
	else
	  printf("Failure, player did not discard both cards\n");
      } else {
	if(contains_card(state.discard[0], card1, 3) == 1)
	  pass++;
	else
	  printf("Failure, player did not discard first choice card\n");
	if(contains_card(state.discard[1], card2, 3) == 1)
	  pass++;
	else
	  printf("Failure, player did not discard second choice card\n");
      }
    }

    if(card_count(state.discard[0], steward, 3) == 1)
      pass++;
    else
      printf("Failure, player did not discard steward\n");

    if(pass == 6)
      printf("Success, all tests completed without errors\n");
  }
}

int main()
{
  steward_test();
  return 0;
}
